{extend name='template/base'}{/extend}
{block name='style'}{/block}
{block name='script'}{/block}
{block name='content'}
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-body">
                <div id="toolbar">
                    <div class="btn-group pull-left mr10">
                        <a class="btn btn-primary" id="add_btn"><i class="fa fa-plus"></i> 新增管理员</a>
                    </div>
                </div>
                <table id="table"
                       data-toggle="table"
                       data-show-refresh="true"
                       data-side-pagination="server"
                       data-pagination="true"
                       data-url="{:url('Admin/index_data')}"
                >
                    <thead>
                    <tr>
                        <th data-field="nick">昵称</th>
                        <th data-field="role_name">角色分组</th>
                        <th data-field="flag_lock" data-formatter="formatStatus">状态</th>
                        <th data-field="name">姓名</th>
                        <th data-field="mobile">电话</th>
                        <th data-field="reg_time" data-formatter="formatTime">注册时间</th>
                        <th data-field="reg_ip">注册ip</th>
                        <th data-field="last_login_time" data-formatter="formatTime">最后登录时间</th>
                        <th data-field="last_login_ip">最后登录ip</th>
                        <!--<th data-field="login_count">累计登录次数</th>-->
                        <th data-field="operate" data-formatter="formatOperate" data-events="operateEvents" >操作</th>
                    </tr>
                    </thead>
                </table>
            </div><!-- /.box-body -->
        </div><!-- /.box -->

    </div><!-- /.col -->
</div><!-- /.row -->
{/block}
{block name='script_extra'}
<script>
    var add_data_url="{:url('Admin/add')}";
    var edit_data_url="{:url('Admin/edit')}";
    var delete_data_url="{:url('Admin/delete')}";
    var changePassword_data_url="{:url('Admin/change_password')}";
</script>
<script>
    // <editor-fold defaultstate="collapsed" desc="格式化url">
    function urlFormat(url) {
        if( url.indexOf('.html')){
            url=url.replace('.html','')
        }
        return url;
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="添加">
        $('#add_btn').on('click',function () {
            layer_open('添加',add_data_url)
        });
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="操作按钮">
    function formatOperate(value,row,index){
        if(row.id !=1){
            return [
                '<button class="btn btn-success btn-xs row_role" href="javascript:void(0)">',
                '<i class="fa  fa-expeditedssl"></i> 分配角色',
                '</button>&nbsp;',
                '<button class="btn btn-primary btn-xs row_changePassword" href="javascript:void(0)">',
                '<i class="fa fa-edit"></i> 更改密码',
                '</button>&nbsp;',
                '<button class="btn btn-warning btn-xs row_edit" href="javascript:void(0)">',
                '<i class="fa fa-edit"></i> 修改',
                '</button>&nbsp;',
                '<button class="btn btn-danger btn-xs row_delete" href="javascript:void(0)">',
                '<i class="fa fa-trash"></i> 删除',
                '</button>'
            ].join('');
        }

    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="操作函数">
    window.operateEvents = {
        'click .row_role': function (e, value, row, index) {
            layer_open('分配角色',urlFormat("{:url('Admin/role')}")+"/id/"+row.id);
        },
        'click .row_changePassword': function (e, value, row, index) {
            layer_open('编辑',urlFormat(changePassword_data_url)+"/id/"+row.id);
        },
        'click .row_edit': function (e, value, row, index) {
            layer_open('更改密码',urlFormat(edit_data_url)+"/id/"+row.id);
        },
        'click .row_delete': function (e, value, row, index) {
            layer.confirm('你确定要删除该选项吗？',{btn:['确定','取消']},function () {
                $.ajax({
                    dataType:'json',
                    data:{id:row.id},
                    url:delete_data_url,
                    success:function (res) {
                        if(res.success){
                            layer.msg(res.message, {time: 1000},function () {
                                $('#table').bootstrapTable('refresh');
                            });
                        }else{
                            layer.alert(res.message);
                        }
                    }
                })
            })
        },
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="时间格式化">
    function formatTime(value) {
        if(value){
            var date = new Date(parseInt(value) * 1000);
            Y = date.getFullYear() + '-';
            M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            D = (date.getDate() + 1 < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
            h = (date.getHours() + 1 < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
            m = (date.getMinutes() + 1 < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
            s = (date.getSeconds() + 1 < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
            return Y + M + D + h + m + s;
        }

    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="状态格式化">
    function formatStatus(value) {
        switch(value){
            case 0:
                return '<i class="fa fa-check-circle text-green" title="正常">正常</i>';break;
            case 1:
                return '<i class="fa fa-minus-circle text-yellow" title="锁定">锁定</i>';break;
        }

    }
    // </editor-fold>

</script>
{/block}